﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@Html.Partial("_YibaoCommonView")
<form name="form1">
    <div class="panel panel-default" style="margin-bottom: 0%;background-color:lavender">
        <div class="panel-heading navb-bg">
            住院医保患者结算回退
        </div>
        <div style="padding-top:1%;padding-bottom:1%; padding-left:7%;" id="divInpatientBasicInfo">
            <table class="form">
                <tr>
                    <th class="formTitle">prm_yka103：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="yka103" style="float:left;" />
                    </td>
                    <th class="formTitle">住院号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="zyh" style="float:left;" />
                    </td>
                    <td class="formValue">
                        <input type="button" id="btn_jsht" class="btn btn-primary" style="width:40%;margin-left:10px;" value="结算回退" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="panel panel-default" style="margin-bottom: 0%;">
        <div class="panel-heading navb-bg">
            不确定交易筛选条件
        </div>
        <div style="padding-top:1%;padding-bottom:1%; padding-left:7%;" id="divInpatientBasicInfo">
            <table class="form">
                <tr>

                    <td class="formValue">
                        <input type="text" class="form-control" id="jybh" style="float:left;" />
                    </td>
                    <td class="formValue" colspan="2">
                        <input type="button" id="btn_search" class="btn btn-primary" style="width:15%; margin-left:10px;" value="查询" />
                        <input type="button" id="btn_confirm" class="btn btn-primary" style="width:15%;margin-left:10px;" value="执行" />
                        <input type="button" id="btn_cancel" class="btn btn-primary" style="width:15%;margin-left:10px;" value="撤销" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="gridPanel" style="margin-top:1%">
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>
</form>
<script>
    //查询
    $('#btn_search').click(function() {
        loadDataResult();
    });
    //执行
    $('#btn_confirm').click(function() {
        confirm_yb();
    });
    //撤销
    $('#btn_cancel').click(function() {
        cancel_yb();
    });
    $('#btn_jsht').click(function () {
        ybjsht();
    });

    function ybjsht() {
        var yka103 = $("#yka103").val();
        var zyh = $("#zyh").val();
        if (!yka103) {
            $.modalAlert("请输入医保结算返回值prm_yka103", 'warning');
            return;
        }
        if (!zyh) {
            $.modalAlert("请输入住院号", 'warning');
            return;
        }
        $.najax({
            url: '/HospitalizationManage/DischargeSettle/GetInpatientSettYbPatInfo?zyh=' + zyh,
            loadingtext: "正在请求患者出院信息，请稍后...",
            type: 'POST',
            success: function (ybPreSettleReq) {
                var ybjshtReturn;
                var jshtData = {
                    prm_akc190: ybPreSettleReq.data.jzbh,
                    prm_aac001: ybPreSettleReq.data.sbbh,
                    prm_yab003: ybPreSettleReq.data.fzxbh,
                    prm_aka130: ybPreSettleReq.data.zhifuleibie,
                    prm_yka103: yka103,
                    prm_aae011: "",
                    prm_aae036: $.getTime(),
                    prm_aae013: "重新计费",
                    prm_ykb065: ybPreSettleReq.data.sbbf,
                    prm_ykc141: "GF001"
                };
                $.ajax({
                    type: "POST",
                    url: "http://127.0.0.1:12345/api/YiBao/ZyFeejsht",
                    contentType: 'application/json',
                    data: JSON.stringify(jshtData),
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        ybjshtReturn = eval('(' + data + ')');
                    }
                });
                if (ybjshtReturn.Code == 0) {
                    $.modalAlert("结算回退成功！", 'success');
                    var parameter1 = { "astr_jylsh": ybjshtReturn.jylsh, "astr_jyyzm": ybjshtReturn.jyyzm };
                    $.ajax({
                        url: "http://127.0.0.1:12345/api/YiBao/YibaoConfirm",
                        data: parameter1,
                        dataType: "json",
                        async: false,
                        success: function (data) {
                        }
                    });
                } else {
                    $.modalAlert(ybjshtReturn.ErrorMsg, 'error');
                }

                var ybReturn;
                var mnjsData = {
                    prm_akc190: ybPreSettleReq.data.jzbh,
                    prm_aac001: ybPreSettleReq.data.sbbh,
                    prm_yab003: ybPreSettleReq.data.fzxbh,
                    prm_aka130: ybPreSettleReq.data.zhifuleibie,
                    prm_aae036: "",
                    prm_ykb065: ybPreSettleReq.data.sbbf,
                    prm_ykc141: "GF001"
                };
                $.ajax({
                    type: "POST",
                    url: "http://127.0.0.1:12345/api/YiBao/ZyCyblht",
                    contentType: 'application/json',
                    data: JSON.stringify(mnjsData),
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        ybReturn = eval('(' + data + ')');
                    }
                });

                if (ybReturn.Code == 0) {
                    $.modalAlert("取消成功！", 'success');
                    var parameter = { "astr_jylsh": ybReturn.jylsh, "astr_jyyzm": ybReturn.jyyzm };
                    $.ajax({
                        url: "http://127.0.0.1:12345/api/YiBao/YibaoConfirm",
                        data: parameter,
                        dataType: "json",
                        async: false,
                        success: function (data) {
                        }
                    });
                } else {
                    $.modalAlert(ybReturn.ErrorMsg, 'error');
                }
            }
        });
    }


    //ajax加载数据
    function loadDataResult() {
        //清空Grid
        $('#gridList').jqGrid("clearGridData");
        /*
        var str = '{"output": {"sqldata": {"row": [{"yke014": "2000000001","yke015": "48"},' +
            '{"yke014": "2000000002","yke015": "48"},{"yke014": "2000000003","yke015": "48"},' +
            '{"yke014": "2000000004","yke015": "21"},{"yke014": "2000000005","yke015": "48"}]}}} ';
        var yb_getuncertaintytrade = eval('(' + str + ')');
        yb_getuncertaintytrade.output.sqldata.row.push('{ "yke014": "", "yke015": "" }');
        $('#gridList').jqGrid('setGridParam', {
            datatype: 'local',
            data: yb_getuncertaintytrade.output.sqldata.row
        }).trigger("reloadGrid");
        */

        var jybh = $.trim($("#jybh").val());
        var yb_getuncertaintytradeInfo;// = $.guianyibao.YibaoGetUncertaintyTrade(jybh);
        var parameter = { "astr_jybh": jybh };
        $.ajax({
            url: "http://127.0.0.1:12345/api/YiBao/YibaoGetUncertaintyTrade", 
            data: parameter,
            dataType: "json",
            async: false,
            success: function (data) {
                yb_getuncertaintytradeInfo = eval('(' + data + ')');
            }
        });
        if (yb_getuncertaintytradeInfo && yb_getuncertaintytradeInfo.Code == 0) {
            var yb_getuncertaintytrade = yb_getuncertaintytradeInfo.Data;
            if (!yb_getuncertaintytrade) {
                $.modalAlert("还没有不确定交易情况", 'succsee');
                return;
            }
            var nullobj= {
                yke014: "", yke015: ""
            }
            yb_getuncertaintytrade.push(nullobj);
            $('#gridList').jqGrid('setGridParam',
                {
                    datatype: 'local',
                    data: yb_getuncertaintytrade
                }).trigger("reloadGrid");
        } else
            $.modalAlert("获取医保不确定交易数据失败", 'error');
    }

    window.newtouch_globalconfig.f4opions = {
        container: "#divInpatientBasicInfo,#divPatInfo"
    };

    //不确定交易
    $('#gridList').newtouchLocalDataGrid({
        caption: '不确定交易',
        height: $(window).height() - 180,
        unwritten: false,
        editurl: "clientArray",
        colModel: [
            { label: "交易编号", name: "yke015", width: 100, align: "left" },
            { label: "交易流水号", name: "yke014", width: 180, align: "left" }
        ],
        multiselect: true
    });

    function confirm_yb() {
        //获取选中行Id
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds.length == 0) {
            $.modalAlert("请先选中需处理的信息", 'warning');
            return;
        }
        if (!confirm("是否确认处理选中的信息?")) {
            return;
        }
        var IsSuccess = true;
        //判断退数量
        for (var i = 0; i < selRowIds.length; i++) {
            var rowData = $("#gridList").jqGrid('getRowData', selRowIds[i]);
            if (!rowData) {
                $.modalAlert("获取信息失败，请重新操作", 'error');
                break;
            }
            $.najax({
                url: "/OutpatientManage/DealGuiAnNotConfirm/GetJyyzmByJylsh",
                data: { jylsh: rowData["yke014"] },
                dataType: 'text',
                async: false,
                success: function (rep) {
                    if (rep == "") {
                        IsSuccess = false;
                        $.modalAlert("系统中不存在流水号【" + rowData["yke014"] + "】的交易记录，请确认。", 'warning');
                    } else {
                        alert(rep);
                        try {
                            var ybjySettReturn;// = $.guianyibao.YibaoConfirm(rowData["yke014"], rep);
                            var parameter = { "astr_jylsh": rowData["yke014"], "astr_jyyzm": rep };
                            $.ajax({
                                url: "http://127.0.0.1:12345/api/YiBao/YibaoConfirm", 
                                data: parameter,
                                dataType: "json",
                                async: false,
                                success: function (data) {
                                    ybjySettReturn = eval('(' + data + ')');
                                }
                            });
                            if (!ybjySettReturn || !(ybjySettReturn.Code == 0)) {
                                $.modalAlert("流水号【" + jylsh + "】提交失败，请重新操作", 'error');
                                IsSuccess = false;
                            }
                        } catch (err) {
                            IsSuccess = false;
                            $.modalAlert("医保提交失败，请重新操作" + err, 'error');
                        }
                    }
                }
            });
            if (!IsSuccess) {
                loadDataResult();
                return;
            }
        }
        $.modalAlert("处理完成", 'success');
        loadDataResult();
    }

    function cancel_yb() {
        //获取选中行Id
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds.length == 0) {
            $.modalAlert("请先选中需处理的信息", 'warning');
            return;
        }
        if (!confirm("是否确认撤销选中的信息?")) {
            return;
        }

        //判断退数量
        for (var i = 0; i < selRowIds.length; i++) {
            var rowData = $("#gridList").jqGrid('getRowData', selRowIds[i]);
            if (!rowData) {
                $.modalAlert("获取信息失败，请重新操作", 'error');
                break;
            }
            var ybjySettReturn;// = $.guianyibao.YibaoCancle(rowData["yke014"]);
            var parameter = { "astr_jylsh": rowData["yke014"] };
            $.ajax({
                url: "http://127.0.0.1:12345/api/YiBao/YibaoCancle", 
                data: parameter,
                dataType: "json",
                async: false,
                success: function (data) {
                    ybjySettReturn = eval('(' + data + ')');
                }
            });
            if (!(ybjySettReturn.Code == 0)) {
                $.modalAlert("流水号【" + jylsh + "】撤销失败，请重新操作", 'error');
                loadDataResult();
                return;
            }
        }
        $.modalAlert("处理完成", 'success');
        loadDataResult();
    }

</script>
